package test.demo.support;

import cn.hutool.core.bean.BeanException;
import test.demo.BeanFactory;
import test.demo.BeansException;
import test.demo.config.BeanDefinition;
import test.demo.config.SingletonBeanRegistry;

/**
 * @Description:
 * @author: JC
 * @date: 2023/4/4 23:05
 */
public abstract class AbstractBeanFactory extends DefaultSingletonBeanRegistry implements BeanFactory {
    @Override
    public Object getBean(String name) {
        Object singleton = getSingleton(name);
        if(singleton != null){
            return singleton;
        }
        BeanDefinition beanDefinition = getBeanDefinition(name);
        return createBean(name, beanDefinition, null);
    }

    @Override
    public Object getBean(String beanName, Object... args) throws BeansException {
        Object singleton = getSingleton(beanName);
        if(singleton != null){
            return singleton;
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        return createBean(beanName, beanDefinition, args);
    }

    protected abstract Object createBean(String name, BeanDefinition beanDefinition, Object[] args) throws BeanException;

    protected abstract BeanDefinition getBeanDefinition(String name);
}
